package net.wang.totakecodenotes.wechatofficaccounts.utils;

import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 *
 */
public class HttpUtils {

    private static final Logger logger = Logger.getLogger(HttpUtils.class);

    public static String  getJSONString(String url) throws IOException {
        return  new String(getByteArray(url));
    }

    public static byte[] getByteArray(String url) throws IOException {
        logger.info("url = " + url);
        URL u=new URL(url);
        InputStream in=u.openStream();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        try {
            byte buf[]=new byte[1024];
            int read = 0;
            while ((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }
        }  finally {
            if (in != null) {
                in.close();
            }
        }
        return out.toByteArray( );
    }
}
